import pandas as pd

t = pd.Timestamp('2023-10-17')
t = t.replace(day=1)
index = pd.date_range(start=t, end=(t + pd.offsets.MonthEnd()))
df = pd.DataFrame(index.weekday+1 , index= index.date , columns=['weekday'])
# print(df.head(10))

df = df.query('weekday == 3' ).tail(1).index[0]
print(df)